package com.hmy.finance.service.receipt.impl;

import com.alibaba.fastjson.JSON;
import com.hmy.ccp.error.exception.HMYRuntimeException;
import com.hmy.finance.dal.mapper.receipt.CustomerBalanceAccountMapper;
import com.hmy.finance.dal.po.receipt.CustomerBalanceAccountPo;
import com.hmy.finance.service.receipt.CustomerBalanceAccountProvideService;
import com.hmy.finance.service.receipt.dto.customerAccount.CustomerBalanceQueryDto;
import com.hmy.finance.service.receipt.dto.customerAccount.CustomerInitialAccountDto;
import com.hmy.finance.service.receipt.rdto.customerAccount.CustomerBalanceQueryRdto;
import com.hmy.finance.service.receipt.rdto.customerAccount.CustomerInitialAccountRdto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Service
public class CustomerBalanceAccountProvideServiceImpl implements CustomerBalanceAccountProvideService {
    @Resource
    private CustomerBalanceAccountMapper customerBalanceAccountMapper;

    @Override
    public CustomerInitialAccountRdto initialCustomerBalanceAccount(CustomerInitialAccountDto customerInitialAccountDto) {
        // check Customer Balance Account Exist
        CustomerBalanceAccountPo customerBalanceAccount = customerBalanceAccountMapper.selectById(customerInitialAccountDto.getCustomerId());
        if (Objects.nonNull(customerBalanceAccount)) {
            CustomerInitialAccountRdto customerInitialAccountRdto = new CustomerInitialAccountRdto();
            customerInitialAccountRdto.setCustomerId(customerInitialAccountDto.getCustomerId());
            customerInitialAccountRdto.setResult(true);
            customerInitialAccountRdto.setMessage("Customer Balance Account Exist");
            return customerInitialAccountRdto;
        }
        // Initial Customer Balance Account
        customerBalanceAccount = new CustomerBalanceAccountPo();
        customerBalanceAccount.setBalanceAmount(BigDecimal.ZERO);
        customerBalanceAccount.setCustomerId(customerInitialAccountDto.getCustomerId());
        customerBalanceAccount.setIsDeleted(0);
        customerBalanceAccount.setCustomerName(customerInitialAccountDto.getCustomerName());
        customerBalanceAccount.setCreateTime(LocalDateTime.now());
        customerBalanceAccount.setUpdateTime(LocalDateTime.now());
        customerBalanceAccount.setCreatorId(-1L);
        customerBalanceAccount.setUpdaterId(-1L);
        customerBalanceAccount.setCreatorName("system");
        customerBalanceAccount.setUpdaterName("system");
        log.info("CustomerBalanceAccountService#initialCustomerBalanceAccount request:{}", JSON.toJSON(customerInitialAccountDto));
        int inserted = customerBalanceAccountMapper.insert(customerBalanceAccount);
        if (inserted <= 0) {
            throw new HMYRuntimeException(506400007);
        }
        CustomerInitialAccountRdto customerInitialAccountRdto = new CustomerInitialAccountRdto();
        customerInitialAccountRdto.setCustomerId(customerInitialAccountDto.getCustomerId());
        customerInitialAccountRdto.setResult(true);
        customerInitialAccountRdto.setMessage("Initial Customer Balance Account Success");
        log.info("CustomerBalanceAccountService#initialCustomerBalanceAccount response:{}", JSON.toJSON(customerInitialAccountRdto));
        return customerInitialAccountRdto;
    }

    @Override
    public CustomerBalanceQueryRdto queryCustomerBalance(CustomerBalanceQueryDto customerBalanceQueryDto) {
        CustomerBalanceAccountPo customerBalanceAccount = customerBalanceAccountMapper.selectById(customerBalanceQueryDto.getCustomerId());
        if (Objects.nonNull(customerBalanceAccount)) {
            CustomerBalanceQueryRdto customerBalanceQueryRdto = new CustomerBalanceQueryRdto();
            customerBalanceQueryRdto.setCustomerId(customerBalanceAccount.getCustomerId());
            customerBalanceQueryRdto.setBalanceAmount(customerBalanceAccount.getBalanceAmount());
            return customerBalanceQueryRdto;
        }
        return null;
    }
}
